package 
{
	/**
	 * ...
	 * @author
	 */
	 import laya.display.Sprite;
	 import laya.display.Animation;
	 import laya.events.Event;
	 import laya.maths.Rectangle;
	 import laya.utils.Handler;

	public class Role extends Sprite {

		public var type:String;
		public var hp:Number = 0;
		public var speed:Number = 0;
		public var roleAni:Animation;
		public var action:String;

		public function Role() {
			roleAni = new Animation();
			roleAni.loadAnimation("GameRole.ani");
		}

		public function init(type:String, hp:Number, speed:Number): void {
			this.type = type;
			this.hp = hp;
			this.speed = speed;
			this.addChild(roleAni);

			roleAni.on(Event.COMPLETE, this, onComplete);

			playAction("fly");
		}

		public function playAction(action:String): void {
			this.action = action;
			roleAni.play(0, true, this.type + "_" + action);
		}

		private function onComplete():void {
			//如果角色还未有宽，获得角色宽高    
            if(roleAni.width==0)
            {
                //获得动画矩形边界
                var bounds:Rectangle=roleAni.getBounds();
                //角色宽高赋值
                roleAni.size(bounds.width,bounds.height)
            }
		}

		public function update():void {
			//主角边界检查
            if(this.type=="hero")
            {
                //需减去角色宽或高的一半，因为在IDE中制作动画时，我们把角色的中心做为了角色对象的原点
                //判断是否左右超出
                if(this.x<roleAni.width/2)
                {
                    this.x=roleAni.width/2;
                }
                else if(this.x>720-roleAni.width/2)
                {
                    this.x=720-roleAni.width/2;
                }
                //判断是否上下超出
                if(this.y<roleAni.height/2)
                {
                    this.y=roleAni.height/2;
                }
                else if(this.y>1280-roleAni.height/2)
                {
                    this.y=1280-roleAni.height/2;
                }
            }
		}
	}

}